JavaScript syntax
part 17/43 Β· 161.3 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// With the constructor
myArray = new Array(0, 1, 2, 3, 4, 5); // length of 6
myArray = new Array(365); // an empty array with length 365
Arrays are implemented so that only the defined elements use memory;
they are "sparse arrays". Setting myArray[10] = 'someThing' and
myArray[57] = 'somethingOther' only uses space for these two elements,
just like any other object. The length of the array will still be
reported as 58. The maximum length of an array is 4,294,967,295 which
corresponds to 32-bit binary number (11111111111111111111111111111111)2.
One can use the object declaration literal to create objects that behave
much like associative arrays in other languages:
const dog = {color: "brown", size: "large"};
dog["color"]; // results in "brown"
dog.color; // also results in "brown"
One can use the object and array declaration literals to quickly create
arrays that are associative, multidimensional, or both. (Technically,
JavaScript does not support multidimensional arrays, but one can mimic
them with arrays-of-arrays.)
const cats = [{color: "brown", size: "large"},
{color: "black", size: "small"}];
cats[0]["size"]; // results in "large"
const dogs = {rover: {color: "brown", size: "large"},
spot: {color: "black", size: "small"}};
dogs["spot"]["size"]; // results in "small"
dogs.rover.color; // results in "brown"
Date
A Date object stores a signed millisecond count with zero representing
1970-01-01 00:00:00 UT and a range of Β±108 days. There are several ways
of providing arguments to the Date constructor. Note that months are
zero-based.
new Date(); // create a new Date instance representing the current
time/date.
new Date(2010, 2, 1); // create a new Date instance representing
2010-Mar-01 00:00:00
new Date(2010, 2, 1, 14, 25, 30); // create a new Date instance
representing 2010-Mar-01 14:25:30
new Date("2010-3-1 14:25:30"); // create a new Date instance from a
String.
Methods to extract fields are provided, as well as a useful toString:
const d = new Date(2010, 2, 1, 14, 25, 30); // 2010-Mar-01 14:25:30;
// Displays '2010-3-1 14:25:30':
console.log(d.getFullYear() + '-' + (d.getMonth() + 1) + '-' +
d.getDate() + ' '
+ d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds());
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ